package org.example.app.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.example.system.vo.BookmarkVo;
import org.example.system.service.BookmarkService;
import org.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 收藏控制层
 */
@RestController
@RequestMapping("/app/bookmark")
@Slf4j
public class BookmarkController {

    @Autowired
    private BookmarkService bookmarkService;

    /**
     * 按条件分页查询
     * @param bookmark
     * @return
     */
    @GetMapping("/list")
    public R list(BookmarkVo bookmark){
        log.info("------------BookmarkController | list | input : {} ", JSON.toJSONString(bookmark));
        IPage<BookmarkVo> page = bookmarkService.selectPage(bookmark);
        log.info("------------BookmarkController | list | return : {} ", JSON.toJSONString(page));
        return R.ok(page);
    }

    /**
     * 添加收藏
     * @param bookmark
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody BookmarkVo bookmark){
        log.info("---------BookmarkController | add | input : {}", JSON.toJSON(bookmark));
        int row = bookmarkService.add(bookmark);
        if(row <= 0){
            return R.fail("添加失败");
        }
        return R.ok("添加成功");
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping( "/delete/{ids}")
    public R delete(@PathVariable("ids") Long[] ids){
        log.info("---------BookmarkController | delete | input : {}", JSON.toJSON(ids));
        int row = bookmarkService.deleteBatch(ids);
        if(row <= 0){
            return R.fail("删除失败");
        }
        return R.ok("删除成功");
    }
}
